package com.itheima.demo06PreparedStatement实现增删改;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
目标:使用PreparedStatement实现增删改

讲解:
	添加数据: 向user表添加1条记录

	注意:只有字段值不确定才能给?
 */
public class Demo06 {
    public static void main(String[] args) throws SQLException {
        // 1.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql:///day18", "root", "root");
        // 2.编写参数化SQL(带?的SQL语句)
        String sql = "INSERT INTO user VALUES (NULL, ?, ?);";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        // 3.给?赋值   pstmt.setXxx(第几个?, ?的具体值)
        pstmt.setString(1, "jack");
        pstmt.setString(2, "123");

        // 4.执行
        int row = pstmt.executeUpdate();
        System.out.println("row1 = " + row);

        row = pstmt.executeUpdate();
        System.out.println("row2 = " + row);

        // 5.释放资源
        pstmt.close();
        conn.close();
    }
}
